
import { Animation, AnimationClip } from 'cc'
import { StateMachine, getInitParamTrigger } from "../../Base/StateMachine"
import { State } from '../../Base/State'
import { WeaponManager } from './WeaponManager'
import { StateTypeEnum } from '../../Common'

export class WeaponStateMachine extends StateMachine {

    entity: WeaponManager
    init(entity: WeaponManager) {
        this.animationComponent = this.node.addComponent(Animation)
        this.entity = entity
        this.initParam()
        this.initState()
        this.initAnimationEvent()
    }

    initParam() {
        this.params.set(StateTypeEnum.idle, getInitParamTrigger())
        this.params.set(StateTypeEnum.shoot, getInitParamTrigger())
    }

    initState() {
        this.stateMachines.set(StateTypeEnum.idle, new State(this, this.entity.type, StateTypeEnum.idle))
        this.stateMachines.set(StateTypeEnum.shoot, new State(this, this.entity.type, StateTypeEnum.shoot, AnimationClip.WrapMode.Normal))
    }

    initAnimationEvent() {
        this.animationComponent.on(Animation.EventType.FINISHED, () => {
            if (this.animationComponent.defaultClip.name == StateTypeEnum.shoot.toString()) {
                this.getComponent(WeaponManager).state = StateTypeEnum.idle
            }
        })
    }

    run() {

        switch (this.currencyState) {
            case this.stateMachines.get(StateTypeEnum.idle):
            case this.stateMachines.get(StateTypeEnum.shoot):
                if (this.params.get(StateTypeEnum.idle).value) {
                    this.currencyState = this.stateMachines.get(StateTypeEnum.idle)
                } else if (this.params.get(StateTypeEnum.shoot).value) {
                    this.currencyState = this.stateMachines.get(StateTypeEnum.shoot)
                }
                break;
            default:
                this.currencyState = this.stateMachines.get(StateTypeEnum.idle)
                break;
        }

    }

}